﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using WROA.Models.Entitys;
using WROA.Models.IServices;

namespace WROA.Models.Services
{
    public class CarService : Services, ICarService
    {
        /// <summary>
        /// 用户获取待审批的用车流程
        /// </summary>
        /// <param name="name"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public XPCollection GetAllApplyCar(string name, string position)
        {
            GroupOperator goProcedure1 = new GroupOperator();
            goProcedure1.Operands.Add(new BinaryOperator("Pr_type", "人员", BinaryOperatorType.Equal));
            goProcedure1.Operands.Add(new BinaryOperator("Pr_typevalue", name, BinaryOperatorType.Equal));
            GroupOperator goProcedure2 = new GroupOperator();
            goProcedure2.Operands.Add(new BinaryOperator("Pr_type", "职位", BinaryOperatorType.Equal));
            goProcedure2.Operands.Add(new BinaryOperator("Pr_typevalue", position, BinaryOperatorType.Equal));
            GroupOperator goProcedure = new GroupOperator(GroupOperatorType.Or, goProcedure1, goProcedure2);

            XPCollection xpProcedure = new XPCollection(typeof(Procedure), goProcedure);
            XPCollection xpApplyCar = null;
            foreach (Procedure p in xpProcedure)
            {
                GroupOperator go = new GroupOperator();
                go.Operands.Add(new BinaryOperator("Procedure", p, BinaryOperatorType.Equal));
                go.Operands.Add(new BinaryOperator("Ac_state", "未结束", BinaryOperatorType.Equal));
                XPCollection xp = new XPCollection(typeof(ApplyDocument), go);
                xpApplyCar.AddRange(xp);
            }

            return xpApplyCar;
        }

        /// <summary>
        /// 根据id返回一个用车审批对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApplyCar GetApplyCar(int id)
        {
            ApplyCar applyCar = null;
            XPCollection xp = new XPCollection(typeof(ApplyCar), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (ApplyCar ac in xp)
            {
                applyCar = ac;
            }
            return applyCar;
        }

        public Car GetCar(int id)
        {
            Car car = null;
            XPCollection xp = new XPCollection(typeof(Car), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (Car c in xp)
            {
                car = c;
            }
            return car;
        }

        /// <summary>
        /// 根据id更新用车申请流程为已经结束
        /// </summary>
        /// <param name="id"></param>
        public void UpdateApplyCarState(int id)
        {
            XPCollection xp = new XPCollection(typeof(ApplyCar), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (ApplyCar ac in xp)
            {
                ac.Ac_state = "已结束";
                ac.Save();
            }
        }

        /// <summary>
        /// 根据id更新公文申请流程已经批准
        /// </summary>
        /// <param name="id"></param>
        public void UpdateApplyCarIsAgree(int id)
        {
            XPCollection xp = new XPCollection(typeof(ApplyCar), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (ApplyCar ac in xp)
            {
                ac.Ac_isagree = "已批准";   //相反为不批准
                ac.Save();
            }
        }

        /// <summary>
        /// 保存公文审批信息，如果不批准流程，则修改公文申请表状态
        /// </summary>
        /// <param name="documentApprove"></param>
        public void SaveCarApprove(CarApprove carApprove)
        {
            if (carApprove.Ca_result == "不批准")
            {
                carApprove.Applycar.Ac_state = "结束";
                carApprove.Applycar.Ac_isagree = "不批准";
            }
            carApprove.Save();
        }

        /// <summary>
        /// 判断汽车是否在特定时间内是否空闲
        /// </summary>
        /// <param name="name"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public bool CarIsFree(int Oid, DateTime startTime, DateTime endTime)
        {
            bool result = true;
            Car car = null;
            XPCollection xpCar = new XPCollection(typeof(Car), new BinaryOperator("Oid", Oid, BinaryOperatorType.Equal));
            foreach (Car c in xpCar)
            {
                car = c;
            }
            XPCollection xp = car.applycardatas;
            foreach (ApplyCarData acd in xp)
            {
                if (acd.Applycar.Ac_state == "未开始")//已结束
                {   //如果开始时间和结束时间在会议申请信息表开始时间和结束时间中间
                    if ((DateTime.Compare(startTime, acd.Acd_starttime) >= 0 && DateTime.Compare(startTime, acd.Acd_endtime) <= 0) || (DateTime.Compare(endTime, acd.Acd_starttime) >= 0 && DateTime.Compare(endTime, acd.Acd_endtime) <= 0))
                    {
                        result = false;
                    }
                    //如果开始时间在会议申请信息表开始时间之前且结束时间在会议申请信息表结束时间之后
                    if (DateTime.Compare(startTime, acd.Acd_starttime) < 0 && DateTime.Compare(endTime, acd.Acd_endtime) > 0)
                    {
                        result = false;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 保存用车人员名单
        /// </summary>
        /// <param name="applyCarData"></param>
        /// <param name="name"></param>
        public void SavePassage(ApplyCarData applyCarData, List<string> name)
        {
            UserService us = new UserService();
            for (int i = 0; i < name.Count; i++)
            {
                Passenger passenger = new Passenger();
                passenger.Applycardata = applyCarData;
                passenger.User = us.GetUser(name[i]);
                passenger.Save();
            }
        }
    }
}